<?php
/**
 *
 * ClassName: Htmlsearchparser_model
 *
 * description...
 *
 * @author lizhifeng <lizhifeng@soften.cn>
 *
 */
 
class Hotparser_model extends CI_Model{

	var $wbfield = array(
			'_id'			 =>'',//微博ID
			//'idstr'		     =>'',//字符串型的微博ID
			'text'		     =>'',//微博信息内容
			'ctime'		     =>'',//微博创建时间
			'source'		 =>'',//微博来源
			'reposts_count'  =>'',//转发数
			'comments_count' =>'',//评论数
			'attitudes_count'=>'',//表态数(好评数)
			'retweeted_id'	 =>'',//转载的微博ID
			'retweeted_uid'	 =>'',//转载的微博用户ID
			'retweeted_name' =>'',//转载的微博用户名
			'url'			 =>'',//详情URL
			'thumbnail_pic'  =>'',//缩略图片地址，没有时不返回此字段
			'bmiddle_pic'	 =>'',//中等尺寸图片地址，没有时不返回此字段
			'original_pic'   =>'',//原始图片地址，没有时不返回此字段
			'addtime'		 =>'',//增加时间
			'source_myid'	 =>'',//渠道ID抓取渠道
			'user' => Array(
					'id'				=>'',//用户ID
					'profile_url'	    =>'',//用户空间URL
					'name'	            =>'',//昵称
					'description'       =>'',//描述
					'header_url'        =>'',//头像
                    'followers_count'   =>'',//粉丝数
					'friends_count'     =>'',//关注数
					'verified'		    =>'',//是否加V
				)
		 );


	public function __construct() 
	{
		parent::__construct();
		$this->load->helper('MY_common_helper');
        $this->load->helper('MY_http_helper');
	}


 	public  function sina_hot_html_parser($html)
    {   		 	
        $Patten='/<div  tbinfo=(?:.*?)mid="(?P<mid>.*?)"(?:.*?)<div class="WB_face W_fl">(?:.*?)href="(?P<profile_url>.*?)"(?:.*?)title="(?P<name>.*?)"(?:.*?)<img(?:.*?)src="(?P<header_url>.*?)"(?:.*?)node-type="feed_list_content">(?P<content>.*?)<\/div>(?P<imglist>.*?)<div class="WB_from S_txt2">(?:.*?)href="(?P<url>.*?)"(?:.*?)date="(?P<ctime>.*?)"(?:.*?)<\/a>(?:.*?)<a(?:.*?)>(?P<source>.*?)<(?:.*?)node-type="forward_btn_text">(?P<reposts_count>.*?)<(?:.*?)node-type="comment_btn_text">(?P<comments_count>.*?)<(?:.*?)<em>(?P<attitudes_count>.*?)<(?:.*?)<div node-type="feed_list_repeat" class="WB_feed_repeat S_bg1" style="display:none;">/' ;   
    	$html=$this->PreHand($html);	
    	$imgPatten='/<img(?:.*?)src="(?P<image>.*?)"(?:.*?)>/' ; 	
		$p1='#http://weibo.com/(.*?)/(.*)#' ;
		$par = "/[\x80-\xff]/";
        $patternContent='/<div class="WB_feed" pageNum="" node-type=\'feed_list\' feed-type="1">.*?<\/script>/';
		   
    	if(preg_match_all($Patten,$html,$Match))
    	{
    		$count=count($Match[0]);
    		for($i=0;$i<$count;$i++)
    		{
    			$rTemp=array();
    			$rTemp['_id']=$Match['mid'][$i] ;
    			$Wcontent=$Match['content'][$i] ;
    			$Wcontent=preg_replace('/<img render="ext"(.*?)title="(.*?)"(.*?)>/','${2}',$Wcontent);
    			$rTemp['text']=$Wcontent ;
    			$imgList=$Match['imglist'][$i];		
    			preg_match_all($imgPatten,$imgList,$imgMatch) ;
    			$imgCount=count($imgMatch[0]);
    			$SmallimgListStr='';
    			for($j=0;$j<$imgCount;$j++)
    			{
    				$temp=stripslashes($imgMatch['image'][$j]);
    				$SmallimgListStr=$SmallimgListStr.$temp.'*' ;
    			}    		
    			$rTemp['thumbnail_pic']=$SmallimgListStr;   		    		
    			$rTemp['bmiddle_pic']=str_replace('thumbnail', 'bmiddle', $SmallimgListStr);  		
    			$rTemp['bmiddle_pic']=str_replace('square', 'bmiddle', $rTemp['bmiddle_pic']);	 		
    			$rTemp['original_pic']=str_replace('bmiddle', 'large', $rTemp['bmiddle_pic']);    		
    			$rTemp['ctime']=$Match['ctime'][$i] ; 		
    			
    			
    			@$rTemp['ctime']=substr($Match['ctime'][$i],0,10);
    			//$rTemp['ctime']=str_replace('000','',$Match['ctime'][$i]) ;
    			
    			$rTemp['source']=$Match['source'][$i];		
    			$rTemp['reposts_count']=preg_replace($par,"",$Match['reposts_count'][$i]) ;		
    			$rTemp['comments_count']=preg_replace($par,"",$Match['comments_count'][$i]);		
    			$rTemp['attitudes_count']=$Match['attitudes_count'][$i] ;  		
    			$rTemp['url']=$Match['url'][$i]; 		
    			$user=array();	
    			preg_match_all($p1,$rTemp['url'],$m);		
    			$user['id']=$m[1][0];		
    			$user['name']= $Match['name'][$i] ;		
    			$user['profile_url']=$Match['profile_url'][$i];
    			$user['header_url']=$Match['header_url'][$i];
    			if(!strpos($Match[0][$i],'verified'))
    			{
    				$user['verified']=0 ;	
    			}
    			else 
    			{
    				$user['verified'] = 1;
    			}			
    			$user['profile_url']=$Match['profile_url'][$i];
    			$rTemp['user']=$user ;	
    			$rTemp['addtime'] = time();	
    			$result[]=$rTemp ; 		
    		}	 
    		return  $result ;
    	}
    	else 
    	{
    		log_message ( 'error', "can not Extraction message list from hotparser_model", FALSE, 'hot/sina' );
    		return false;
    		// 如果无法匹配到微博列表  可能原因是抓取的页面不正确 或者 是网页改版所导致
    	}
    }
     
	private function PreHand($str)
	{	
		$str=preg_replace('/^\s*$/m', '', $str);
		$str=preg_replace('/\n{2}/', "\n", $str);
		// 删除空行
		$str = str_replace("\r\n","",$str);
		$str = str_replace("\r","",$str);
		$str = str_replace("\n","",$str);	
		$str = str_replace("\\r\\n","",$str);
		$str = str_replace("\\r","",$str) ;
		$str = str_replace("\\n","",$str);
		$str = str_replace("\\t","",$str);
		$str=stripslashes($str);
		return trim($str);
	}
	
}

?>
